<script setup>
import { ref, watch } from 'vue';
import { useCounterStore } from '@/store';
import { onLaunch, onShow } from '@dcloudio/uni-app'; // 引入应用生命周期
import request from '@/utils/request';

const store = useCounterStore();

watch(
  () => store.open_id,
  (val) => {
    if (!val) {
      if (store.is_type == 1) {
        uni.reLaunch({
          url: '/pages/login/index',
        });
      } else if (store.is_type == 2) {
        uni.reLaunch({
          url: '/pages/masterlogin/index',
        });
      }
      uni.removeStorageSync('is_type');
      store.is_type = '';
    }
  },
);

// 登录
const getUserByOpenId = async () => {
  try {
    const data = await request('wxapp/login/getUserByOpenId');
    store.userInfo = data;
    if (store.userInfo.status === 2) {
      yhfoutLoginFun();
      store.outFun();
      uni.reLaunch({
        url: '/pages/login/index',
      });
    }
  } catch {
    store.outFun();
    uni.reLaunch({
      url: '/pages/login/index',
    });
  }
};

const getWorkerByOpenId = async () => {
  try {
    const data = await request('worker/login/getWorkerByOpenId');
    store.workerData = data;
    if (store.workerData.status === 4) {
      sfoutLoginFun();
      store.outFun();
      uni.reLaunch({
        url: '/pages/master/home/index',
      });
    }
  } catch {
    store.outFun();
    uni.reLaunch({
      url: '/pages/masterlogin/index',
    });
  }
};
// 退出登录
const sfoutLoginFun = async () => {
  await request('worker/login/outLogin');
  uni.showToast({
    title: '您的账号已被禁用，请联系客服处理',
    icon: 'none',
  });
};
// 退出登录
const yhfoutLoginFun = async () => {
  await request('wxapp/login/outLogin');
  uni.showToast({
    title: '您的账号已被禁用，请联系客服处理',
    icon: 'none',
  });
};
const checkLogin = () => {
  if (store.is_type == 1) {
    getUserByOpenId();
  }
  if (store.is_type == 2) {
    getWorkerByOpenId();
    if (store.open_id && store.phone) {
      uni.reLaunch({
        url: '/pages/master/home/index',
      });
    }
  }
};

onLaunch(() => {
  checkLogin(); // 每次应用显示时都检查登录状态
  uni.addInterceptor('navigateTo', {
    success(args) {
      const pages = getCurrentPages();
      // 取最后一个元素（当前页面）
      const currentPage = pages[pages.length - 1];
      // 获取当前页面路径（如：pages/index/index）
      const currentPath = currentPage.route;

      console.log('跳转了navigateTo', currentPath);
      clearInterval(store.times);
      if (store.is_type == 1) {
        getUserByOpenId();
      } else if (store.is_type == 2) {
        getWorkerByOpenId();
      }
    },
  });
  uni.addInterceptor('redirectTo', {
    success(args) {
      const pages = getCurrentPages();
      // 取最后一个元素（当前页面）
      const currentPage = pages[pages.length - 1];
      // 获取当前页面路径（如：pages/index/index）
      const currentPath = currentPage.route;

      console.log('跳转了redirectTo', currentPath);
      clearInterval(store.times);
      if (store.is_type == 1) {
        getUserByOpenId();
      } else if (store.is_type == 2) {
        getWorkerByOpenId();
      } else if (
        ![
          'pages/login/index',
          'pages/login/index2',
          'pages/masterlogin/index',
          'pages/masterlogin/index2',
        ].includes(currentPath)
      ) {
        store.outFun();
        uni.reLaunch({
          url: '/pages/login/index',
        });
      }
    },
  });
  uni.addInterceptor('reLaunch', {
    success(args) {
      const pages = getCurrentPages();
      // 取最后一个元素（当前页面）
      const currentPage = pages[pages.length - 1];
      // 获取当前页面路径（如：pages/index/index）
      const currentPath = currentPage.route;

      console.log('跳转了reLaunch', currentPath);
      clearInterval(store.times);
      if (store.is_type == 1) {
        getUserByOpenId();
      } else if (store.is_type == 2) {
        getWorkerByOpenId();
      } else if (
        ![
          'pages/login/index',
          'pages/login/index2',
          'pages/masterlogin/index',
          'pages/masterlogin/index2',
        ].includes(currentPath)
      ) {
        store.outFun();
        uni.reLaunch({
          url: '/pages/login/index',
        });
      }
    },
  });
  uni.getSetting({
    withSubscriptions: true,
    success(res) {
      store.subscriptionsSetting = res.subscriptionsSetting;
    },
  });
});
</script>

<style lang="scss">
@import '@/uni.scss';

.page-container {
  height: 100vh;
  background: #eaf2fc; /* 页面背景色 */
  display: flex;
  flex-direction: column;
  font-size: 28rpx;
}

.page_content {
  flex: 1;
}
.page-container {
  color: $uni-text-color;
}
.flex {
  display: flex;
  align-items: center;
}
.flex_center {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}
.flex_jb {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.fixed {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  z-index: 4;
}
.fuxbox rich-text {
  width: 100%;
}
.mr {
  margin-right: 10rpx;
}
.border_b {
  border-bottom: 1rpx solid #eee;
}
.border {
  border: 1rpx solid #eee;
}
#contactBtn,
.contactBtn {
  display: block;
  /* 清除背景和边框 */
  background: transparent;
  border: none;
  /* 清除圆角 */
  border-radius: 0;
  /* 清除内边距（根据需求调整） */
  padding: 0;
  /* 清除默认尺寸 */
  height: auto;
  /* 清除文字样式 */
  color: inherit; /* 继承父元素文字颜色 */
  font-size: inherit; /* 继承父元素文字大小 */
  /* 清除点击态（重点） */
  -webkit-tap-highlight-color: transparent; /* 清除点击高亮 */
  line-height: normal;
  &::after {
    display: none;
  }
}

.over_text_200 {
  display: block;
  width: 400rpx;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}
</style>
